<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/29
 * Time: 19:09
 */
namespace App\Http\Controllers\TimedTask;

use Carbon\Carbon;

use App\Models\Bill\Bill; //订单表

use App\Http\Controllers\Controller;

use App\Models\BackstageIndex; //后台统计表

use App\Models\Wx\WxCustomer; //微信粉丝表

use App\Models\Customer\Customer; //会员表

use App\Models\Statistic\StatDateCust; //会员统计表 - 按日期

use App\Models\Statistic\StatDateSaleBill; //订单每日销售统计

use App\Models\Statistic\StatDateWechatUser; //微信会员统计表 - 按日期

class BackstageIndexStatController extends Controller
{

    //统计后台首页数据
    public function stat()
    {
        $yesterday_date = date('Y-m-d',strtotime('-1 day')); //昨日
        $startTime = date('Y-m-d').' 00:00:00'; //统计开始时间
        $endTime = date('Y-m-d').' 23:59:59'; //统计结束时间

        //统计微信粉丝关注数
        $today_fans_data = WxCustomer::whereBetween('createTime',[$startTime,$endTime])->count();//统计今日微信关注数
        $fans_data = StatDateWechatUser::select('newFans','totalCustNum')->where([['date',$yesterday_date],['channel_type',1]])->get()->toArray(); //查询昨日微信关注数和累计关注数

        //统计会员数
        $today_customer_data = Customer::whereBetween('createTime',[$startTime,$endTime])->count();//统计今日会员数
        $customer_data = StatDateCust::select('newCustNum','totalCustNum')->where([['date',date('Ymd',strtotime('-1 day'))],['channel_type',1]])->get()->toArray();//查询昨日会员数和累计会员数

        //统计订单数
        $today_bill_data = Bill::whereBetween('createTime',[$startTime,$endTime])->count();//统计今日订单数
        $yesterday_bill_data = Bill::whereBetween('createTime',[$yesterday_date.' 00:00:00',$yesterday_date.' 23:59:59'])->count();//统计昨日订单数
        $total_bill_data = Bill::where('createTime','<=',$yesterday_date.' 23:59:59')->count();

        //统计销售数
        $today_sale_data = Bill::whereBetween('createTime',[$startTime,$endTime])->where('bill_status',4)->count();//统计今日销售量
        $yesterday_sale_data = StatDateSaleBill::select('trade_amount')->where('cal_date',$yesterday_date)->get()->toArray();//查询昨日销售量
        $total_sale_data = StatDateSaleBill::sum('trade_amount'); //统计累计销售量

        //保存统计数据
        $statistics_backstage_index = BackstageIndex::find(1);
        if ( empty($statistics_backstage_index) ) {//新增

            $statistics_backstage_index = new BackstageIndex();
            $statistics_backstage_index->uuid = makeUuid();
            $statistics_backstage_index->timeStamp = Carbon::now();
            $statistics_backstage_index->creator = 'system';
            $statistics_backstage_index->createTime = Carbon::now();
            $statistics_backstage_index->id = 1;

        }

        $statistics_backstage_index->timeStamp = Carbon::now();
        $statistics_backstage_index->wx_subscribe_num = $today_fans_data;
        $statistics_backstage_index->yesterday_wx_subscribe_num = !empty($fans_data) ? $fans_data[0]['newFans'] : 0;
        $statistics_backstage_index->total_wx_subscribe_num = !empty($fans_data) ? $fans_data[0]['totalCustNum'] : 0;
        $statistics_backstage_index->customer_num = $today_customer_data;
        $statistics_backstage_index->yesterday_customer_num = !empty($customer_data) ? $customer_data[0]['newCustNum'] : 0;
        $statistics_backstage_index->total_customer_num = !empty($customer_data) ? $customer_data[0]['totalCustNum'] : 0;
        $statistics_backstage_index->bill_num = $today_bill_data;
        $statistics_backstage_index->yesterday_bill_num = $yesterday_bill_data;
        $statistics_backstage_index->total_bill_num = $total_bill_data;
        $statistics_backstage_index->sale_num = $today_sale_data;
        $statistics_backstage_index->yesterday_sale_num = !empty($yesterday_sale_data) ? $yesterday_sale_data[0]['trade_amount'] : 0;
        $statistics_backstage_index->total_sale_num = !empty($total_sale_data) ? (int)$total_sale_data : 0;
        $statistics_backstage_index->save();

        return response()->json(['code' => 200 , 'message' => 'ok' ]);

    }

}
